home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 93 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.5 KB

  1. Path: fido.asd.sgi.com!austern
  2. From: Etay_Bogner@mail.stil.scitex.com (Etay Bogner)
  3. Newsgroups: comp.std.c++
  4. Subject: Re: FW: Inherent C++ problem?
  5. Date: 22 Jan 1996 09:52:13 PST
  6. Organization: Scitex Corp.
  7. Approved: austern@isolde.mti.sgi.com
  8. Message-ID: <Etay_Bogner-2101961132190001@metay.stil.scitex.com>
  9. References: <01BAE696.8C249300@dino.int.com>
  10. NNTP-Posting-Host: isolde.mti.sgi.com
  11. X-Original-Date: Sun, 21 Jan 1996 11:32:19 +0200
  12. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  13.     iQBVAwUBMQPO5Ey4NqrwXLNJAQGtNwH9HTwdbR0yppid4lpgGv3bWV16vbGsAmM3
  14.     +iu3+IwNm7hcEJh/rI1J1vM2sFuq73WiVh48+k0EqcwgcOYNoou9UA==
  15.     =z8VJ
  16. Originator: austern@isolde.mti.sgi.com
  17.  
  18. In article <01BAE696.8C249300@dino.int.com>, Eugene Lazutkin
  19. <eugene@int.com> wrote:
  20.  
  21. >> ----------
  22. >> From:   Max Motovilov[SMTP:max@int.com]
  23. >> Sent:   Friday, January 19, 1996 4:01 PM
  24. >> To:     'Eugene Lazutkin'
  25. >> Subject:        (for comp.std.c++)  Inherent C++ problem?
  26. >> 
  27. >> It is all right by now. Consider the following use then:
  28. >> 
  29. >>         Complex a( 1, 0 ), b( 0, 1 );
  30. >>         Complex c( a+b );
  31. >> 
  32. >> In the last line 3 objects of type Complex are constructed instead of 1, 
  33. >> namely:
  34. >> 
  35. >> 1) Temporary Complex object constructed in 'operator +'
  36. >> 2) Return value of 'operator +' copy-constructed from (1)
  37. >> 3) 'c', copy-constructed from (2)
  38. >> 
  39.  
  40. For simplicity, let's write this as :
  41.  
  42. Complex callee() {
  43.     return Complex(0,0);
  44.     }
  45.  
  46. void caller() {
  47.     Complex a = callee(); // which is basically the same as a(callee())
  48.     }
  49.  
  50. There are two points of interest :
  51. 1) the "caller" function
  52. 2) the "callee" function
  53.  
  54. Let's see what the compilers knows at each point :
  55.  
  56. 1) the compiler knows it's going to call a function the returns an object.
  57. So it might make some space on the stack for the returned object and put a
  58. pointer to this space somewhere in the argument list to the "callee()"
  59. function.
  60.  
  61. 2) the compiler know that this function is going to return an object, so
  62. it expects to find a pointer to place the newly created in. it indeed
  63. creates a new object but it directly creates it in the "callee()" stack
  64. space.
  65.  
  66. So now the, your example is actually doing only one step :
  67.  
  68. operator+ create the newly created "a+b" object directly in c.
  69.  
  70. -- 
  71. -- Etay Bogner,
  72. -- Etay_Bogner@mail.stil.scitex.com,
  73. -- Scitex Corp.
  74. -- Israel.
  75. ---
  76. [ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  77.   Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  78.   is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
  79.